<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%B8%89-lazy-free/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">537 字 ~3分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<blockquote>
<p>源文地址：<a href="https://www.jianshu.com/p/e927e99e650d">https://www.jianshu.com/p/e927e99e650d</a></p>
</blockquote>

<h1 id="redis4-0新特性-三-lazy-free">Redis4.0新特性(三)-Lazy Free</h1>

<p>Redis4.0新增了非常实用的lazy free特性，从根本上解决Big Key(主要指定元素较多集合类型Key)删除的风险。笔者在redis运维中也遇过几次Big Key删除带来可用性和性能故障。
本文分为以下几节说明redis lazy free：</p>

<ul>
<li>lazy free的定义</li>
<li>我们为什么需要lazy free</li>
<li>lazy free的使用</li>
<li>lazy free的监控</li>
<li>lazy free实现的简单分析</li>
</ul>

<h3 id="lazy-free的定义">lazy free的定义</h3>

<p>lazy free可译为惰性删除或延迟释放；当删除键的时候,redis提供异步延时释放key内存的功能，把key释放操作放在bio(Background I/O)单独的子线程处理中，减少删除big key对redis主线程的阻塞。有效地避免删除big key带来的性能和可用性问题。</p>

<h3 id="我们为什么需要lazy-free">我们为什么需要lazy free</h3>

<p>Redis是single-thread程序(除少量的bio任务),当运行一个耗时较大的请求时，会导致所有请求排队等待redis不能响应其他请求，引起性能问题,甚至集群发生故障切换。</p>

<p>而redis删除大的集合键时，就属于这类比较耗时的请求。通过测试来看，删除一个100万个元素的集合键，耗时约1000ms左右。
以下测试，删除一个100万个字段的hash键，耗时1360ms；处理此DEL请求期间，其他请求完全被阻塞。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#204a87;font-weight:bold">删除一个100万字段的hash键</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">HLEN</span> <span style="color:#204a87;font-weight:bold">hlazykey</span>
<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1000000</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">del</span> <span style="color:#204a87;font-weight:bold">hlazykey</span>
<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span>
<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">36s</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">SLOWLOG</span> <span style="color:#204a87;font-weight:bold">get</span>
<span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">0</span>
   <span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1501314385</span>
   <span style="color:#204a87;font-weight:bold">3</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1360908</span>
   <span style="color:#204a87;font-weight:bold">4</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;del&#34;</span>
      <span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;hlazykey&#34;</span>
   <span style="color:#204a87;font-weight:bold">5</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;127.0.0.1:35595&#34;</span>
   <span style="color:#204a87;font-weight:bold">6</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#a40000">“</span><span style="color:#a40000">&#34;</span></code></pre></div>
<p>测试估算，可参考；和硬件环境、Redis版本和负载等因素有关</p>

<table>
<thead>
<tr>
<th>Key类型</th>
<th>Item数量</th>
<th>耗时</th>
</tr>
</thead>

<tbody>
<tr>
<td>Hash</td>
<td>~100万</td>
<td>~1000ms</td>
</tr>

<tr>
<td>List</td>
<td>~100万</td>
<td>~1000ms</td>
</tr>

<tr>
<td>Set</td>
<td>~100万</td>
<td>~1000ms</td>
</tr>

<tr>
<td>Sorted Set</td>
<td>~100万</td>
<td>~1000ms</td>
</tr>
</tbody>
</table>

<p>在redis4.0前，没有lazy free功能；DBA只能通过取巧的方法，类似scan big key,每次删除100个元素；但在面对“被动”删除键的场景，这种取巧的删除就无能为力。
例如：我们生产Redis Cluster大集群，业务缓慢地写入一个带有TTL的2000多万个字段的Hash键，当这个键过期时，redis开始被动清理它时，导致redis被阻塞20多秒，当前分片主节点因20多秒不能处理请求，并发生主库故障切换。</p>

<p>redis4.0有lazy free功能后，这类主动或被动的删除big key时，和一个O(1)指令的耗时一样,亚毫秒级返回； 把真正释放redis元素耗时动作交由bio后台任务执行。</p>

<h3 id="lazy-free的使用">lazy free的使用</h3>

<p>lazy free的使用分为2类：第一类是与DEL命令对应的主动删除，第二类是过期key删除、maxmemory key驱逐淘汰删除。</p>

<h4 id="主动删除键使用lazy-free">主动删除键使用lazy free</h4>

<h5 id="unlink命令">UNLINK命令</h5>

<p><a href="https://link.jianshu.com/?t=https://redis.io/commands/unlink">UNLINK命令</a>是与DEL一样删除key功能的lazy free实现。
唯一不同时，UNLINK在删除集合类键时，如果集合键的元素个数大于64个(详细后文），会把真正的内存释放操作，给单独的bio来操作。
示例如下：使用UNLINK命令删除一个大键mylist, 它包含200万个元素，但用时只有0.03毫秒</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">7000</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">LLEN</span> <span style="color:#204a87;font-weight:bold">mylist</span>
<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">2000000</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">7000</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">UNLINK</span> <span style="color:#204a87;font-weight:bold">mylist</span>
<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">7000</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">SLOWLOG</span> <span style="color:#204a87;font-weight:bold">get</span>
<span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span>
   <span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1505465188</span>
   <span style="color:#204a87;font-weight:bold">3</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">integer</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">30</span>
   <span style="color:#204a87;font-weight:bold">4</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;UNLINK&#34;</span>
      <span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;mylist&#34;</span>
   <span style="color:#204a87;font-weight:bold">5</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;127.0.0.1:17015&#34;</span>
   <span style="color:#204a87;font-weight:bold">6</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;&#34;</span></code></pre></div>
<p>注意：DEL命令，还是并发阻塞的删除操作</p>

<h5 id="flushall-flushdb-async">FLUSHALL/FLUSHDB ASYNC</h5>

<p>通过对<a href="https://link.jianshu.com/?t=https://redis.io/commands/flushall">FLUSHALL/FLUSHDB</a>添加ASYNC异步清理选项，redis在清理整个实例或DB时，操作都是异步的。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">127.0.0.1:7000&gt; DBSIZE
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1812295</span>
127.0.0.1:7000&gt; flushall  //同步清理实例数据，180万个key耗时1020毫秒
OK
<span style="color:#ce5c00;font-weight:bold">(</span>1.02s<span style="color:#ce5c00;font-weight:bold">)</span>
127.0.0.1:7000&gt; DBSIZE
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1812637</span>
127.0.0.1:7000&gt; flushall async  //异步清理实例数据，180万个key耗时约9毫秒
OK
127.0.0.1:7000&gt; SLOWLOG get
 1<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">2996109</span>
    2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1505465989</span>
    3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">9274</span>       //指令运行耗时9.2毫秒
    4<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;flushall&#34;</span> 
       2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;async&#34;</span>
    5<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;127.0.0.1:20110&#34;</span>
    6<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;&#34;</span></code></pre></div>
<h4 id="被动删除键使用lazy-free">被动删除键使用lazy free</h4>

<p>lazy free应用于被动删除中，目前有4种场景，每种场景对应一个配置参数； 默认都是关闭。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#000">lazyfree</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#204a87;font-weight:bold">lazy</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">eviction</span> <span style="color:#000">no</span>
<span style="color:#000">lazyfree</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#204a87;font-weight:bold">lazy</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">expire</span> <span style="color:#000">no</span>
<span style="color:#000">lazyfree</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#204a87;font-weight:bold">lazy</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">server</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">del</span> <span style="color:#000">no</span>
<span style="color:#000">slave</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#204a87;font-weight:bold">lazy</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">flush</span> <span style="color:#000">no</span></code></pre></div>
<p>注意：从测试来看lazy free回收内存效率还是比较高的； 但在生产环境请结合实际情况，开启被动删除的
lazy free 观察redis内存使用情况。</p>

<h5 id="lazyfree-lazy-eviction">lazyfree-lazy-eviction</h5>

<p>针对redis内存使用达到maxmeory，并设置有淘汰策略时；在被动淘汰键时，是否采用lazy free机制；
因为此场景开启lazy free, 可能使用淘汰键的内存释放不及时，导致redis内存超用，超过maxmemory的限制。此场景使用时，请结合业务测试。</p>

<h5 id="lazyfree-lazy-expire-todo-验证这类操作-同步到从库的是del还是unlink">lazyfree-lazy-expire &ndash;todo 验证这类操作 同步到从库的是DEL还是UNLINK.</h5>

<p>针对设置有TTL的键，达到过期后，被redis清理删除时是否采用lazy free机制；
此场景建议开启，因TTL本身是自适应调整的速度。</p>

<h5 id="lazyfree-lazy-server-del">lazyfree-lazy-server-del</h5>

<p>针对有些指令在处理已存在的键时，会带有一个隐式的DEL键的操作。如<a href="https://link.jianshu.com/?t=https://redis.io/commands/rename">rename命令</a>，当目标键已存在,redis会先删除目标键，如果这些目标键是一个big key,那就会引入阻塞删除的性能问题。 此参数设置就是解决这类问题，建议可开启。</p>

<h5 id="slave-lazy-flush">slave-lazy-flush</h5>

<p>针对slave进行全量数据同步，slave在加载master的RDB文件前，会运行flushall来清理自己的数据场景，
参数设置决定是否采用异常flush机制。如果内存变动不大，建议可开启。可减少全量同步耗时，从而减少主库因输出缓冲区爆涨引起的内存使用增长。</p>

<h3 id="lazy-free的监控">lazy free的监控</h3>

<p>lazy free能监控的数据指标，只有一个值：lazyfree_pending_objects，表示redis执行lazy free操作，在等待被实际回收内容的键个数。并不能体现单个大键的元素个数或等待lazy free回收的内存大小。
所以此值有一定参考值，可监测redis lazy free的效率或堆积键数量； 比如在flushall async场景下会有少量的堆积。</p>

<h3 id="lazy-free实现的简单分析">lazy free实现的简单分析</h3>

<p>antirez为实现lazy free功能，对很多底层结构和关键函数都做了修改；该小节只介绍lazy free的功能实现逻辑；代码主要在源文件lazyfree.c和bio.c中。</p>

<h4 id="unlink命令-1">UNLINK命令</h4>

<p>unlink命令入口函数unlinkCommand()和del调用相同函数delGenericCommand()进行删除KEY操作，使用lazy标识是否为lazyfree调用。如果是lazyfree,则调用dbAsyncDelete()函数。
但并非每次unlink命令就一定启用lazy free，redis会先判断释放KEY的代价(cost),当cost大于LAZYFREE_THRESHOLD才进行lazy free.
释放key代价计算函数lazyfreeGetFreeEffort()，集合类型键，且满足对应编码，cost就是集合键的元数个数，否则cost就是1.
举例：
1 一个包含100元素的list key, 它的free cost就是100
2 一个512MB的string key, 它的free cost是1
所以可以看出，redis的lazy free的cost计算主要时间复杂度相关。</p>

<p>lazyfreeGetFreeEffort()函数代码</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">size_t lazyfreeGetFreeEffort(robj *obj) {
    if (obj-&gt;type == OBJ_LIST) {  
        quicklist *ql = obj-&gt;ptr;
        return ql-&gt;len;
    } else if (obj-&gt;type == OBJ_SET <span style="color:#a40000">&amp;</span><span style="color:#a40000">&amp;</span> obj-&gt;encoding == OBJ_ENCODING_HT) {
        dict *ht = obj-&gt;ptr;
        return dictSize(ht);
    } else if (obj-&gt;type == OBJ_ZSET <span style="color:#a40000">&amp;</span><span style="color:#a40000">&amp;</span> obj-&gt;encoding == OBJ_ENCODING_SKIPLIST){
        zset *zs = obj-&gt;ptr;
        return zs-&gt;zsl-&gt;length;
    } else if (obj-&gt;type == OBJ_HASH <span style="color:#a40000">&amp;</span><span style="color:#a40000">&amp;</span> obj-&gt;encoding == OBJ_ENCODING_HT) {
        dict *ht = obj-&gt;ptr;
        return dictSize(ht);
    } else {
        return 1; /* Everything else is a single allocation. */
    }
}</code></pre></div>
<p>dbAsyncDelete()函数的部分代码</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">#define LAZYFREE_THRESHOLD 64 //根据FREE一个key的cost是否大于64，用于判断是否进行lazy free调用
int dbAsyncDelete(redisDb *db, robj *key) {
    /* Deleting an entry from the expires dict will not free the sds of
     * the key, because it is shared with the main dictionary. */
    if (dictSize(db-&gt;expires) &gt; 0) dictDelete(db-&gt;expires,key-&gt;ptr); //从expires中直接删除key

    dictEntry *de = dictUnlink(db-&gt;dict,key-&gt;ptr); //进行unlink处理，但不进行实际free操作
    if (de) {
        robj *val = dictGetVal(de);
        size_t free_effort = lazyfreeGetFreeEffort(val); //评估free当前key的代价

        /* If releasing the object is too much work, let&#39;s put it into the
         * lazy free list. */
        if (free_effort &gt; LAZYFREE_THRESHOLD) { //如果free当前key cost&gt;64, 则把它放在lazy free的list, 使用bio子线程进行实际free操作，不通过主线程运行
            atomicIncr(lazyfree_objects,1); //待处理的lazyfree对象个数加1，通过info命令可查看
            bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL); 
            dictSetVal(db-&gt;dict,de,NULL);
        }
    }

}</code></pre></div>
<p>在bio中实际调用lazyfreeFreeObjectFromBioThread()函数释放key</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">lazyfreeFreeObjectFromBioThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">robj</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">decrRefCount</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//更新对应引用，根据不同类型，调用不同的free函数
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">atomicDecr</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">lazyfree_objects</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//完成key的free,更新待处理lazyfree的键个数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<h4 id="flushall-flushdb-async命令">flushall/flushdb async命令</h4>

<p>当flushall/flushdb带上async,函数emptyDb()调用emptyDbAsync()来进行整个实例或DB的lazy free逻辑处理。
emptyDbAsync处理逻辑如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">/* Empty a Redis DB asynchronously. What the function does actually is to
 * create a new empty set of hash tables and scheduling the old ones for
 * lazy freeing. */
void emptyDbAsync(redisDb *db) {
    dict *oldht1 = db-&gt;dict, *oldht2 = db-&gt;expires; //把db的两个hash tables暂存起来
    db-&gt;dict = dictCreate(<span style="color:#ce5c00">&amp;dbDictType,NULL);</span> //为db创建两个空的hash tables
    db-&gt;expires = dictCreate(<span style="color:#ce5c00">&amp;keyptrDictType,NULL);</span>
    atomicIncr(lazyfree_objects,dictSize(oldht1)); //更新待处理lazyfree的键个数，加上db的key个数
    bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,oldht1,oldht2);//加入到bio list
}</code></pre></div>
<p>在bio中实际调用lazyfreeFreeDatabaseFromBioThread函数释放db</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">lazyfreeFreeDatabaseFromBioThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dict</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">ht1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">dict</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">ht2</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">size_t</span> <span style="color:#000">numkeys</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dictSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ht1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">dictRelease</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ht1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">dictRelease</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ht2</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">atomicDecr</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">lazyfree_objects</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">numkeys</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#8f5902;font-style:italic">//完成整个DB的free,更新待处理lazyfree的键个数 
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<h4 id="被动删除键使用lazy-free-1">被动删除键使用lazy free</h4>

<p>被动删除4个场景，redis在每个场景调用时，都会判断对应的参数是否开启，如果参数开启，则调用以上对应的lazy free函数处理逻辑实现。</p>

<h3 id="总结">总结</h3>

<p>因为Redis是单个主线程处理，antirez一直强调&rdquo;Lazy Redis is better Redis&rdquo;.
而lazy free的本质就是把某些cost(主要时间复制度，占用主线程cpu时间片)较高删除操作，从redis主线程剥离，让bio子线程来处理，极大地减少主线阻塞时间。从而减少删除导致性能和稳定性问题。</p>

<p>参考：
<a href="http://antirez.com/news/93">http://antirez.com/news/93</a></p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%B8%80-memory-command/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%BA%8C-psync2/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>